package com.qdlc.p2p.quartz.job;

import java.text.MessageFormat;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.util.StopWatch;

import com.qdlc.p2p.biz.service.TppAccountService;
import com.qdlc.p2p.biz.service.TppBonusDetailsService;
import com.qdlc.p2p.dal.common.SystemConfigHelper;
import com.qdlc.p2p.dal.common.SystemConfigHelper.Nid;
import com.qdlc.p2p.dal.dao.TppBonusDetailsDao;
import com.qdlc.p2p.dal.dao.UserDao;
import com.qdlc.p2p.dal.dto.TppBonusDetails;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.quartz.AbstractJob;

/**
 * 更新新浪存钱罐收益给用户
 *
 * @author wuaj
 * @version 1.1.0.0
 * @since 2016年03月11日
 */
public class QueryBonusDetails extends AbstractJob {

    @Resource
    private TppBonusDetailsService tppBonusDetailsService;

    @Resource
    private TppBonusDetailsDao tppBonusDetailsDao;

    @Resource
    private UserDao userDao;

    @Resource
    private TppAccountService tppAccountService;

    @Override
    protected void process() {
    	
    	StopWatch watch = new StopWatch();
    	if ("baofoo".equals(SystemConfigHelper.getSystemConfig(Nid.TPP_NAME).getValue())) {// 宝付
    		try {
                // 对账JOB START
                tppAccountService.updateStatus();// 处理对账先更新旧数据为已处理
            } catch (Exception ex) {
                ex.printStackTrace();
            }

            // 过滤条件：1.未开通托管账户 2.可用余额为0
            logger.info("处理宝付对账JOB START=======");
            watch.start();
            List<User> hasBonusUserList = userDao.getHasBonusUser();
            for (User user : hasBonusUserList) {
                try {
                    tppAccountService.setBaofooDifference(user);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            watch.stop();
            int size = hasBonusUserList == null ? 0 : hasBonusUserList.size();
            logger.info(MessageFormat.format("宝付对账JOB 结束，处理用户数：[{0}]人,共耗时：[{1}]秒", size, watch.getTotalTimeMillis()));
    	} else {
            // 过滤条件：1.未开通托管账户 2.可用余额为0
            logger.info("查询插入新浪存钱罐收益JOB START=======");
            watch.start();
            List<User> userList = userDao.getHasBonusUser();
            for (User user : userList) {
                // 托管账户收支明细
                try {
                    tppBonusDetailsService.queryBonusDetails(user);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            int size = userList == null ? 0 : userList.size();
            watch.stop();
            logger.info(MessageFormat.format("插入新浪存钱罐收益明细表 JOB 结束，处理用户数：[{0}]人," + "共耗时：[{0}]毫秒", size, watch.getTotalTimeMillis()));

            logger.info("更新新浪存钱罐收益到平台JOB START=======");
            watch.start();
            List<TppBonusDetails> bonusDetails = tppBonusDetailsDao.findByProperty("status", 0);// 所有未更新列表
            for (TppBonusDetails bonusDetail : bonusDetails) {
                try {
                    tppBonusDetailsService.setBonusDetails(bonusDetail);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            watch.stop();
            size = bonusDetails == null ? 0 : bonusDetails.size();
            logger.info(MessageFormat.format("更新新浪存钱罐收益到平台JOB 结束，处理用户数：[{0}]人," + "共耗时：[{1}]秒", size, watch.getTotalTimeMillis()));

            try {
                // 新浪对账JOB START
                tppAccountService.updateStatus();// 处理新浪对账先更新旧数据为已处理
            } catch (Exception ex) {
                ex.printStackTrace();
            }

            // 过滤条件：1.未开通托管账户 2.可用余额为0
            logger.info("处理新浪对账JOB START=======");
            watch.start();
            List<User> hasBonusUserList = userDao.getHasBonusUser();
            for (User user : hasBonusUserList) {
                try {
                    tppAccountService.setDifference(user);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            watch.stop();
            size = hasBonusUserList == null ? 0 : hasBonusUserList.size();
            logger.info(MessageFormat.format("处理存钱罐收益、更新account表(添加资金记录)JOB 结束，处理用户数：[{0}]人,共耗时：[{1}]秒", size, watch.getTotalTimeMillis()));
    	}
    }
}